package com.eims.sls_app.ui.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import bls.juejin.cn.R;

import com.eims.sls_app.dataaccess.AccountDetails;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.ui.MyCommonLotteryInfo;
import com.eims.sls_app.ui.MyCommonLotteryInfo_jc;
import com.eims.sls_app.ui.MyFollowLotteryInfo;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.view.MyListView2;
import com.umeng.socialize.utils.Log;

public class AccountDeatialAdapter extends BaseAdapter {

    private Context context;

    // private List<Schemes>list;

    private List<AccountDetails> list;

    private ViewHolder viewHolder;

    private int mPosition;

    private int childPosition;

    private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

    private OnItemClickListener itemClickListener;

    public AccountDeatialAdapter(Context context, List<AccountDetails> detailsList) {
        super();
        this.context = context;
        list = detailsList;

    }

    public AccountDeatialAdapter(Context context, List<AccountDetails> detailsList, OnItemClickListener listener) {
        super();
        this.context = context;
        list = detailsList;
        this.itemClickListener = listener;

    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {

        if (converView == null) {
            converView = LayoutInflater.from(context).inflate(R.layout.fundsfragment_item, parent, false);

            mPosition = position;

            viewHolder = new ViewHolder();
            viewHolder.tvMonth = (TextView) converView.findViewById(R.id.tv_funfrag_month);
            viewHolder.tvDay = (TextView) converView.findViewById(R.id.tv_funfrag_day);
            viewHolder.lvContent = (MyListView2) converView.findViewById(R.id.funfrag_lv2);

            converView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) converView.getTag();
        }

        // 定义final常量，防止布局在重用时导致position错位

        // final修饰变量时对象引用不变
        final AccountDetails ad = list.get(position); // getview

        // 控件赋值
        String date = ad.date;
        String StrMonth = date.substring(5, 7) + "月";
        String StrDay = date.substring(8);
        viewHolder.tvMonth.setText(StrMonth);
        viewHolder.tvDay.setText(StrDay);

        MyListViewAdapter listViewAdapter = new MyListViewAdapter(ad.dateDetail);
        listViewAdapter.notifyDataSetChanged();

        viewHolder.lvContent.setAdapter(listViewAdapter);

        viewHolder.lvContent.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                Schemes schemes = ad.dateDetail.get(pos);

                // Log.d("haitao", schemes.toString());

                Intent intent = null;

                if (schemes.getOperatorType().equals("2") || schemes.getOperatorType().equals("101")) {
                    if (schemes.getIsChase() == 0) {

                        if ("72".equals(schemes.getLotteryID()) || "73".equals(schemes.getLotteryID() )|| "45".equals(schemes.getLotteryID()))
                            intent = new Intent(context, MyCommonLotteryInfo_jc.class);
                        else
                            intent = new Intent(context, MyCommonLotteryInfo.class);

                    } else {

                        intent = new Intent(context, MyCommonLotteryInfo.class);
                    }

                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    App.scheme = schemes;
                    intent.putExtra("scheme", schemes);
                    context.startActivity(intent);

                } else {
                    Toast.makeText(context, "不支持跳转", Toast.LENGTH_SHORT).show();

                }

            }
        });

        return converView;

    }

    class ViewHolder {

        public TextView tvMonth;

        public TextView tvDay;

        public MyListView2 lvContent;
    }

    class MyListViewAdapter extends BaseAdapter {

        private List<Schemes> schemesList;

        private ViewHolder2 viewHolder2;

        public MyListViewAdapter(List<Schemes> schemesList) {
            super();
            this.schemesList = schemesList;

        }

        // private int childPosition;
        @Override
        public int getCount() {

            return schemesList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return schemesList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View converView, ViewGroup parent) {

            if (converView == null) {
                converView = LayoutInflater.from(context).inflate(R.layout.funsfragment_item_item, parent, false);
                viewHolder2 = new ViewHolder2();

                childPosition = position;

                viewHolder2.tvBid = (TextView) converView.findViewById(R.id.tv_common_bid);
                viewHolder2.tvAccountCJ = (TextView) converView.findViewById(R.id.tv_account_CJ);
                viewHolder2.tvAccountJE = (TextView) converView.findViewById(R.id.tv_account_JE);
                viewHolder2.tvJCTitle = (TextView) converView.findViewById(R.id.tv_JCTitle);
                viewHolder2.tvJCTime = (TextView) converView.findViewById(R.id.tv_JCTime);
                viewHolder2.tvAccountJF = (TextView) converView.findViewById(R.id.tv_account_JF);
                viewHolder2.imArrow = (ImageView) converView.findViewById(R.id.image_accountArrow);

                converView.setTag(viewHolder2);

            } else {

                viewHolder2 = (ViewHolder2) converView.getTag();

            }

            Schemes sch = schemesList.get(position);
            /*
             * if (sch.getType() == 3) { viewHolder2.tvBid.setText("充值"); } else if
			 * (sch.getType() == 4) { viewHolder2.tvBid.setText("提款"); } else {
			 * 
			 * }
			 */

            if (sch.getOperatorType().equals("2") || sch.getOperatorType().equals("101")) {
                viewHolder2.imArrow.setVisibility(View.VISIBLE);
                viewHolder2.tvJCTitle.setText("");
            } else {
                viewHolder2.imArrow.setVisibility(View.GONE);
            }
            viewHolder2.tvBid.setText(sch.getLotteryName());
            // viewHolder2.tvAccountJF.setText(sch.getoType());
            viewHolder2.tvAccountJF.setText("");

            viewHolder2.tvJCTime.setText(sch.getOrderTime());
            viewHolder2.tvAccountJF.setText(sch.getOrderType());
            viewHolder2.tvAccountCJ.setText("彩金变动:" + sch.getHandselMoney() + "元");
            viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");
            if (!sch.getDescription().equals("")) {

                if (sch.getDateTime().equals(sch.getOrderTime())) {
                    viewHolder2.tvAccountCJ.setText("彩金变动:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");
                    // viewHolder2.tvJCTitle.setText("");

                } else {

                    viewHolder2.tvAccountCJ.setText("彩金变动:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");
                    if (sch.getDescription().equals("加奖:0") || sch.getDescription().equals("联盈助手合买单")
                            || sch.getDescription().equals("加奖：0")) {
                        viewHolder2.tvJCTitle.setText("");
                    } else {
                        viewHolder2.tvJCTitle.setText(sch.getDescription() + "元");
                        viewHolder2.tvJCTitle.setTextColor(context.getResources().getColor(R.color.red));
                        TextPaint tp = viewHolder2.tvJCTitle.getPaint();
                        tp.setFakeBoldText(true);
                    }

                }

            }

            if (sch.getSchemeStatus().equals("充值成功")) {
                viewHolder2.tvAccountJF.setText(sch.getOrderType());
                viewHolder2.imArrow.setVisibility(View.GONE);
                viewHolder2.tvBid.setText("充值");
                viewHolder2.tvJCTitle.setText("");
                viewHolder2.tvJCTime.setText(sch.getDateTime());
                viewHolder2.tvAccountCJ.setText("彩金充值:" + sch.getHandselMoney() + "元");
                viewHolder2.tvAccountJE.setText("金额充值:" + sch.getDetailMoney() + "元");
            }
            if (sch.getSchemeStatus().equals("已流单")) {

                if (sch.getOrderType().equals("发红包")) {
                    viewHolder2.tvBid.setText("红包");
                    viewHolder2.tvAccountCJ.setText("红包变动:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");
                } else if (sch.getOrderType().equals("抢红包") || sch.getOrderType().equals("红包退款")) {
                    viewHolder2.tvBid.setText("红包");
                    viewHolder2.tvAccountCJ.setText("红包变动:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");
                } else if (sch.getOrderType().contains("转帐")) {
                    String memo = sch.getMemo();
                    Log.e("str", memo);
                    if (memo.length() > 4) {
                        String str2=memo.substring(0, 3).toString();
                        String str3=memo.substring(memo.length() - 4, memo.length()).toString();
                        Log.e("str1", str2+","+str3);
                        if (sch.getMemo().contains("转帐给")) {

                            String str1 = memo.substring(3, memo.length()).toString();

                            if (isMobileNO(str1)) {

                                String isname = str1.substring(0, 3) + "****" + str1.substring(7, str1.length());
                                viewHolder2.tvBid.setText("转账给"+isname );
                            } else {
                                viewHolder2.tvBid.setText(sch.getMemo());
                            }

                        } else if (sch.getMemo().contains("给你转帐")) {
                            String str1 = memo.substring(0, memo.length() - 4).toString();
                            Log.e("str1", str1);
                            if (isMobileNO(str1)) {

                                String isname = str1.substring(0, 3) + "****" + str1.substring(7, str1.length());
                                viewHolder2.tvBid.setText(isname + "给你转账");
                            } else {
                                viewHolder2.tvBid.setText(sch.getMemo());
                            }
                        } else {

                            viewHolder2.tvBid.setText(sch.getMemo());


                        }


                    }else {
                        viewHolder2.tvBid.setText(sch.getMemo());
                    }


                    viewHolder2.tvAccountCJ.setText("彩金变动:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");


                }else if(sch.getOrderType().equals("返佣")){

                    viewHolder2.tvBid.setText("返佣");
                    viewHolder2.tvAccountCJ.setText("彩金变动:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额变动:" + sch.getDetailMoney() + "元");

                } else {
                    viewHolder2.tvBid.setText("提款");
                    viewHolder2.tvAccountCJ.setText("彩金提款:" + sch.getHandselMoney() + "元");
                    viewHolder2.tvAccountJE.setText("金额提款:" + sch.getDetailMoney() + "元");
                }
                viewHolder2.tvAccountJF.setText(sch.getOrderType());

                viewHolder2.imArrow.setVisibility(View.GONE);
                // viewHolder2.tvJCTitle.setText(sch.getMemo());
                viewHolder2.tvJCTitle.setText("");
                viewHolder2.tvJCTime.setText(sch.getDateTime());

                if (sch.getOrderType().equals("解除提款冻结")) {
                    viewHolder2.tvJCTitle.setText(sch.getMemo());
                }
            }

			/*
			 * if ((sch.getType() == 1) || (sch.getType() == 2)) {
			 * 
			 * viewHolder2.imArrow.setVisibility(View.VISIBLE); } else {
			 * 
			 * viewHolder2.imArrow.setVisibility(View.GONE); }
			 */

			/*
			 * if ((sch.getType() == 3)) {
			 * 
			 * viewHolder2.tvAccountCJ.setText("彩金充值:" + sch.getHandselMoney() + "元");
			 * viewHolder2.tvAccountJE.setText("金额充值:" + sch.getDetailMoney() + "元"); } else
			 * if (sch.getType() == 4) {
			 * 
			 * viewHolder2.tvAccountCJ.setText("彩金提款:" + sch.getHandselMoney() + "元");
			 * viewHolder2.tvAccountJE.setText("金额提款:" + sch.getDetailMoney() + "元"); } else
			 * {
			 * 
			 * viewHolder2.tvAccountCJ.setText("彩金消费:" + sch.getHandselMoney() + "元");
			 * viewHolder2.tvAccountJE.setText("金额消费:" + sch.getDetailMoney() + "元"); }
			 */

			/*
			 * if (sch.getType() == 2) {
			 * viewHolder2.tvJCTitle.setTextColor(context.getResources()
			 * .getColor(R.color.red)); TextPaint tp = viewHolder2.tvJCTitle.getPaint();
			 * tp.setFakeBoldText(true); } else {
			 * 
			 * viewHolder2.tvJCTitle.setTextColor(context.getResources()
			 * .getColor(R.color.black)); TextPaint tp = viewHolder2.tvJCTitle.getPaint();
			 * tp.setFakeBoldText(false); }
			 */

            return converView;
        }

        public boolean isMobileNO(String mobiles) {
            Pattern p = Pattern.compile("1[0-9]{10}");
            Matcher m = p.matcher(mobiles);
            return m.matches();
        }

        class ViewHolder2 {

            public TextView tvBid;

            public TextView tvAccountCJ;

            public TextView tvAccountJE;

            public TextView tvJCTitle;

            public TextView tvJCTime;

            public ImageView imArrow;

            public TextView tvAccountJF;

        }

    }

}
